package org.sky.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.sky.sys.entity.SysCode;
import org.sky.sys.mapper.SysCodeMapper;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.CamelUtils;
import org.sky.util.SnowflakeIdWorker;
import org.sky.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysCommonService
 * @ProjectName springboot-platform
 * @Description: TODO{通用服务业务类}
 * @date 2019/3/8 11:49
 * @Version 1.0
 */
@Service
public class SysCommonService {
    @Autowired
    private SysCommonMapper sysCommonMapper;
    @Autowired
    private SysCodeMapper sysCodeMapper;

    /**
     * 通用下拉选择
     * @param fliter
     * @return
     */
    public List<Map> queryComboData(Map fliter) {
        List<Map> list = sysCommonMapper.queryComboData(fliter);
        return list;
    }

    /**
     * 下拉表格分页查询
     * @param pageNum
     * @param pageSize
     * @param filter
     * @return
     */
    public PageListData selectComboDataByPage(int pageNum, int pageSize,Map filter){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<Map> pageInfo = new PageInfo<Map>(sysCommonMapper.queryComboData(filter));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(CamelUtils.lowerMapKey(pageInfo.getList()));
    }

    /**
     * 返回业务类型+yyyy+8位数字
     * @param type
     * @return
     */
    public synchronized String getCode(String type){
        Date date = sysCommonMapper.querySysDate();
        String year = DateUtils.parseDate(new Date(),"yyyy");
        Example example = new Example(SysCode.class);
        example.createCriteria().andEqualTo("type",type).andEqualTo("date",year);
        SysCode sysCode = sysCodeMapper.selectOneByExample(example);
        Long seq=1L;
        if(null==sysCode){
            sysCode = new SysCode();
            sysCode.setId(SnowflakeIdWorker.getInstance().nextId());
            sysCode.setType(type);
            sysCode.setDate(year);
            sysCode.setSeq(seq);
            sysCode.setCreater(BspUtils.getLoginUser().getCode());
            sysCode.setCreateTime(date);
            sysCode.setUpdater(BspUtils.getLoginUser().getCode());
            sysCode.setUpdateTime(date);
            sysCodeMapper.insertSelective(sysCode);
        }else{
            seq = sysCode.getSeq();
            sysCode.setSeq(seq+1);
            sysCode.setUpdater(BspUtils.getLoginUser().getCode());
            sysCode.setUpdateTime(date);
            example.clear();;
            example.createCriteria().andEqualTo("id",sysCode.getId());
            sysCodeMapper.updateByExampleSelective(sysCode,example);
        }
        return type+year+String.format("%08d", seq);
    }
}
